package io.sage.modules.sys.controller;

import io.sage.common.utils.PageUtils;
import io.sage.common.utils.R;
import io.sage.common.validator.ValidatorUtils;
import io.sage.modules.sys.entity.SysManageEntity;
import io.sage.modules.sys.service.SysManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 系统管理
 */
@RestController
@RequestMapping("sys/manage")
@Api(tags="系统管理接口")
public class SysManageController {
    @Autowired
    private SysManageService sysManageService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions("sys:manage:list")
    @ApiOperation("查询列表")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = sysManageService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 查询所有系统管理条目
     */
    @GetMapping("/all")
    @RequiresPermissions("sys:manage:list")
    @ApiOperation("查询所有")
    public R listAll(){
        return R.ok().put("list", sysManageService.listAll());
    }

    /**
     * 返回存有全部条目的Map，数据结构类型为{"id"，"manage_name"}
     */
    @GetMapping("/listMap")
    @RequiresPermissions("sys:manage:list")
    @ApiOperation("查询全部条目Map")
    public R listMap(){
        List<SysManageEntity> sysManageEntities = sysManageService.listAll();
        Map<String, String> map = new HashMap<>();
        for (SysManageEntity entity: sysManageEntities) {
            map.put(entity.getId()+"",entity.getName());
        }
        return R.ok().put("map", map);
    }
    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("sys:manage:info")
    @ApiOperation("按id查询信息")
    public R info(@PathVariable("id") Integer id){
        //检测id
        if(id==null || id<0)
            return R.error("id 无效！");

        SysManageEntity sysManage = sysManageService.selectById(id);

        return R.ok().put("sysManage", sysManage);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresPermissions("sys:manage:save")
    @ApiOperation("保存")
    public R save(@RequestBody SysManageEntity sysManage){
        sysManageService.save(sysManage);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @RequiresPermissions("sys:manage:update")
    @ApiOperation("更新")
    public R update(@RequestBody SysManageEntity sysManage){
        //检测id
        Integer id = sysManage.getId();
        if(id==null || id<0)
            return R.error("id 无效！");

        ValidatorUtils.validateEntity(sysManage);
        sysManageService.updateById(sysManage);//更新非空非null字段

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @RequiresPermissions("sys:manage:delete")
    @ApiOperation("删除")
    public R delete(@RequestBody Integer[] ids){
        //执行删除
        sysManageService.batchSoftDelete(ids);

        //返回结果
        return R.ok();
    }

}
